package com.smfx.yxt.controller.sys;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.FinalTermScoreReport;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.StudentInfo;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.BaseQueryVO;
import com.smfx.yxt.reqVo.StudentReqVO;
import com.smfx.yxt.service.IFinalTermScoreReportService;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.service.StudentGroupupHandler;

@RequestMapping("sys/student/groupup")
@Controller
public class SysStudentGroupupController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(SysStudentGroupupController.class);
	
	@Autowired
	private IStudentService studentService;
	@Autowired
	private StudentGroupupHandler studentGroupupHandler;
	@Autowired
	private IFinalTermScoreReportService finalTermScoreReportService;
	
	/**
	 * 学生成长足迹
	 * @return
	 */
	@RequestMapping(value = {"", "/"})
	public String index() {
		return "sys/studentGroupup";
	}
	
	@RequestMapping("finalTermScoreReport")
	public String finalTermScoreReport() {
		return "sys/finalTermScoreReport";
	}
	
	@RequestMapping("json/studentList")
	@ResponseBody Object studentList(StudentReqVO vo) {
		try {
			Grid grid = new Grid();
			Page<StudentInfo> page = new Page<>();
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("班号"));
			sorts.add(new OrderBy("学号"));
			page.setSorts(sorts);
			List<StudentInfo> list = studentService.selectFullListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---studentList error={}", e);
		}
		return null;
	}
	
	@RequestMapping("json/batchCreate")
	@ResponseBody Object batchCreate(StudentReqVO vo) {
		try {
			Page<StudentInfo> page = new Page<>();
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(Integer.MAX_VALUE - 1);
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("年级号"));
			sorts.add(new OrderBy("班号"));
			page.setSorts(sorts);
			List<StudentInfo> list = studentService.selectFullListByPage(page);
			studentGroupupHandler.batchCreatePdf(getCurrentTerm().getTermKey(), list);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---batchCreate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("json/createByStudentId")
	@ResponseBody Object createByStudentId(StudentReqVO vo) {
		try {
			StudentInfo student = studentService.selectById(vo.getStudentId());
			if(studentGroupupHandler.createPdf(getCurrentTerm().getTermKey(), student)) {
				return new RspResult(Status.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("---createByStudentId error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("json/getFileList")
	@ResponseBody Object getFileList(StudentReqVO vo) {
		try {
			StudentInfo student = studentService.selectById(vo.getStudentId());
			return studentGroupupHandler.getFileList(student);
		} catch (Exception e) {
			logger.error("---getFileList error={}", e);
		}
		return null;
	}
	
	@RequestMapping("json/queryClassTermReport")
	@ResponseBody Object queryClassTermReport(BaseQueryVO vo) {
		List<FinalTermScoreReport> list = finalTermScoreReportService.selectClassTermReport(vo);
		return list;
	}
	
	@RequestMapping("json/updateStudentTermReport")
	@ResponseBody Object updateStudentTermReport(FinalTermScoreReport record) {
		try {
			finalTermScoreReportService.updateByPrimaryKeySelective(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--updateStudentTermReport error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
}
